package com.sysmanage.permissions.model;


import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;




/** 
  * @ClassName: Resources 
  * @Description:资源信息实体类
  * @author XXXX@163.com 
  * @date 2011-9-27 下午5:55:01 
  *  
  */
@Entity
@Table(name="c_resource")
public class Resources  implements java.io.Serializable {

     private Integer id;
     private String oldResId;

	private Resources parent;
     private Systems system;
     private String name;
     //private String enname;
     private String resourcetype;
     private String link;
     private String icon;
     private Integer iconopen;
     private Integer isopen;
     private Integer isleaf;
     private Integer status;
     private String orderid;
     private String memo;
     private Set<Resources> children = new HashSet<Resources>(0);
     private Set<Actions> resActions = new HashSet<Actions>(0);


    // Constructors

    /** default constructor */
    public Resources() {
    }

	/** minimal constructor */
    public Resources(Integer id, Systems system, String name, Integer isleaf, Integer status) {
        this.id = id;
        this.system = system;
        this.name = name;
        this.isleaf = isleaf;
        this.status = status;
    }
    
    /** full constructor */
  /*  public Resources(Integer id, Resources parent, Systems system, String name, String enname, String resourcetype, String link, String icon, String iconopen, String isopen, String isleaf, String status, Integer orderid, String memo, Set<Resources> children) {
        this.id = id;
        this.parent = parent;
        this.system = system;
        this.name = name;
        this.enname = enname;
        this.resourcetype = resourcetype;
        this.link = link;
        this.icon = icon;
        this.iconopen = iconopen;
        this.isopen = isopen;
        this.isleaf = isleaf;
        this.status = status;
        this.orderid = orderid;
        this.memo = memo;
        this.children = children;
    }*/

  

	// Property accessors
    @Id 
    @Column(name="RES_ID", unique=true, nullable=false)
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Integer getId() {
        return this.id;
    }
    
    /** 
	  * <p>Title: </p> 
	  * <p>Description: </p> 
	  * @param id
	  * @param parent
	  * @param system
	  * @param name
	  * @param enname
	  * @param resourcetype
	  * @param link
	  * @param icon
	  * @param iconopen
	  * @param isopen
	  * @param isleaf
	  * @param status
	  * @param orderid
	  * @param memo
	  * @param children
	  * @param resActions 
	  */ 
	
	public Resources(Integer id, Resources parent, Systems system, String name,
			 String resourcetype, String link, String icon,
			Integer iconopen, Integer isopen, Integer isleaf, Integer status,
			String orderid, String memo, Set<Resources> children) {
		super();
		this.id = id;
		this.parent = parent;
		this.system = system;
		this.name = name;
		
		this.resourcetype = resourcetype;
		this.link = link;
		this.icon = icon;
		this.iconopen = iconopen;
		this.isopen = isopen;
		this.isleaf = isleaf;
		this.status = status;
		this.orderid = orderid;
		this.memo = memo;
		this.children = children;
		//this.resActions = resActions;
	}

	public void setId(Integer id) {
        this.id = id;
    }
	@Column(name="OLD_RES_ID", length=30)
    public String getOldResId() {
		return oldResId;
	}

	public void setOldResId(String oldResId) {
		this.oldResId = oldResId;
	}
	
	
	@ManyToOne(fetch=FetchType.LAZY)
        @JoinColumn(name="parent_id")

    public Resources getParent() {
        return this.parent;
    }
    
    public void setParent(Resources parent) {
        this.parent = parent;
    }
	@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="system_id", nullable=false)
    public Systems getSystem() {
        return this.system;
    }
    
    public void setSystem(Systems system) {
        this.system = system;
    }
    
    @Column(name="cname", nullable=false, length=30)

    public String getName() {
        return this.name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    @Column(name="resource_type", length=2)

    public String getResourcetype() {
        return this.resourcetype;
    }
    
    public void setResourcetype(String resourcetype) {
        this.resourcetype = resourcetype;
    }
    
    @Column(name="link", length=30)

    public String getLink() {
        return this.link;
    }
    
    public void setLink(String link) {
        this.link = link;
    }
    
    @Column(name="icon", length=30)

    public String getIcon() {
        return this.icon;
    }
    
    public void setIcon(String icon) {
        this.icon = icon;
    }
    
    @Column(name="iconopen")

    public Integer getIconopen() {
        return this.iconopen;
    }
    
    public void setIconopen(Integer iconopen) {
        this.iconopen = iconopen;
    }
    
    @Column(name="isopen")

    public Integer getIsopen() {
        return this.isopen;
    }
    
    public void setIsopen(Integer isopen) {
        this.isopen = isopen;
    }
    
    @Column(name="isleaf", nullable=false)

    public Integer getIsleaf() {
        return this.isleaf;
    }
    
    public void setIsleaf(Integer isleaf) {
        this.isleaf = isleaf;
    }
    
    @Column(name="status", nullable=false)

    public Integer getStatus() {
        return this.status;
    }
    
    public void setStatus(Integer status) {
        this.status = status;
    }
    
    @Column(name="orderid", length=10)

    public String getOrderid() {
        return this.orderid;
    }
    
    public void setOrderid(String orderid) {
        this.orderid = orderid;
    }
    
    @Column(name="memo", length=50)

    public String getMemo() {
        return this.memo;
    }
    
    public void setMemo(String memo) {
        this.memo = memo;
    }
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="parent")
    public Set<Resources> getChildren() {
        return this.children;
    }
    
    public void setChildren(Set<Resources> children) {
        this.children = children;
    }

	/**
	 * @return the resActions
	 */
    @ManyToMany(cascade={CascadeType.MERGE})
	@JoinTable(
			name="c_resource_action",
			joinColumns={@JoinColumn(name="res_id")},
			inverseJoinColumns={@JoinColumn(name="action_id")}
	) 
	public Set<Actions> getResActions() {
		return resActions;
	}

	/**
	 * @param resActions the resActions to set
	 */
	public void setResActions(Set<Actions> resActions) {
		this.resActions = resActions;
	}    
	/**
	 * @return the resource
	 */
  
	public String toString()
	{
		return "资源[id:"+this.getId()+",旧ID:"+this.getOldResId()+"name:"+this.getName()+"]";
	}
 

	public int hashCode() {
		return new HashCodeBuilder()
			.append(getId())
			.toHashCode();
	}
	
	public boolean equals(Object obj) {
		if(obj instanceof Resources == false) return false;
		if(this == obj) return true;
		Resources other = (Resources)obj;
		return new EqualsBuilder()
			.append(getId(),other.getId())
			.isEquals();
	}





}